package com.example.project.controller;

import com.example.project.dto.SafetyInspeBinDTO;
import com.example.project.dto.SafetyInspeBinPageDTO;
import com.example.project.dto.SafetyInspePageDTO;
import com.example.project.dto.SafetyTrainBinDTO;
import com.example.project.entity.SafetyInspe;
import com.example.project.entity.SafetyInspeBin;
import com.example.project.entity.SafetyTrainBin;
import com.example.project.service.SafetyInspeBinService;
import com.example.project.service.SafetyInspeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (SafetyInspeBin)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@RestController
@CrossOrigin
@RequestMapping("safetyInspeBin")
public class SafetyInspeBinController {
    /**
     * 服务对象
     */
    @Resource
    private SafetyInspeBinService safetyInspeBinService;


    /**
     * 分页查询
     *
     * @param data 筛选条件
     * @param data      分页对象
     * @return 查询结果
     */

    @PostMapping("findPage")
    public ResponseEntity<Page<SafetyInspeBinPageDTO>> queryByPage(@RequestBody SafetyInspePageDTO data) throws ParseException {
        com.example.project.entity.Page page = data.getPage();
        Date end=null;
        Date star=null;
        if(data.getMyTime2().getEndTime()!=null&&data.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            end=format.parse(data.getMyTime2().getEndTime()+":00");
        }
        if(data.getMyTime2().getStartTime()!=null&&data.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            star =format.parse(data.getMyTime2().getStartTime()+":00");

        }
        return ResponseEntity.ok(this.safetyInspeBinService.queryByPage(data.getSafetyInspe(), PageRequest.of(page.getPage(),page.getSize()), star, end));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @PostMapping("{id}")
    public ResponseEntity<SafetyInspeBin> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.safetyInspeBinService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param safetyInspeBin 实体
     * @return 新增结果
     */

    @PostMapping("add")
    public ResponseEntity<SafetyInspeBin> add(@RequestBody SafetyInspeBin safetyInspeBin) {
        return ResponseEntity.ok(this.safetyInspeBinService.insert(safetyInspeBin));
    }

    /**
     * 编辑数据
     *
     * @param safetyInspeBin 实体
     * @return 编辑结果
     */

    @PutMapping("put")
    public ResponseEntity<SafetyInspeBin> edit(@RequestBody SafetyInspeBin safetyInspeBin) {
        return ResponseEntity.ok(this.safetyInspeBinService.update(safetyInspeBin));
    }

    /**
     * 删除数据
     *
     * param id 主键
     * @return 删除是否成功
     */

    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody SafetyInspeBin data) {
        return ResponseEntity.ok(this.safetyInspeBinService.deleteById1(data.getId()));
    }
    /**
     * 恢复功能
     *
     * @param safetyInspeBin 实体
     * @return 编辑结果
     */

    @PutMapping("restore")
    public ResponseEntity<Boolean> restore(@RequestBody SafetyInspeBin safetyInspeBin) {
        return ResponseEntity.ok(this.safetyInspeBinService.update1(safetyInspeBin));
    }
    /**
     * 批量删除
     * @param data id的字符串
     * @return 删除是否成功
     * */
    @DeleteMapping("delIds")
    public ResponseEntity<Boolean> deleteByIds(@RequestBody SafetyTrainBinDTO data) {
        return ResponseEntity.ok(this.safetyInspeBinService.deleteById2(data));
    }
    /**
     * 批量恢复
     * @param data id的字符串
     * @return 删除是否成功
     * */
    @PutMapping("putIds")
    public ResponseEntity<Boolean> editIds(@RequestBody SafetyTrainBinDTO data){
        return ResponseEntity.ok(this.safetyInspeBinService.update2(data));
    }
}

